package com.cskaoyan.controller.wx.llk;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wx.llk.*;
import com.cskaoyan.service.wx.llk.WxCartService;
import com.cskaoyan.vo.BaseRespVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

//微信小程序购物车
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    @RequestMapping("/index")
    public BaseRespVo wxIndex(){
//        Integer a =1;
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        WxItemsBean<WxCart> wxCartBean = wxCartService.queryIndex(activeUser.getId());
        return BaseRespVo.ok(wxCartBean);
    }

//    @RequestMapping("/index")
//    public BaseRespVo wxIndex(){
////        Integer a =1;
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        WxItemsBean<WxCart> wxCartBean = wxCartService.queryIndex(activeUser.getUserId());
//        return BaseRespVo.ok(wxCartBean);
//    }


    @RequestMapping("/checked")
    public BaseRespVo WxChecked(@RequestBody CheckedBO checkedBO){
//        Integer a =1;

//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        WxItemsBean<WxCart> wxCartBean = wxCartService.updateChecked(checkedBO,activeUser.getId());
        return BaseRespVo.ok(wxCartBean);
    }

    @RequestMapping("/update")
    public BaseRespVo WxCartUpdate(@RequestBody WxCartUpdateBO wxCartUpdateBO){

        wxCartService.WxCartupdate(wxCartUpdateBO);
        return BaseRespVo.ok();
    }
    //购物车商品总数
    @RequestMapping("/goodscount")
    public BaseRespVo WxCartGoodsCount(){
//        Integer a =1;
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        Integer goodsCount = wxCartService.WxCartGoodsCount(activeUser.getId());
        return BaseRespVo.ok(goodsCount);
    }

    @RequestMapping("/add")
    public BaseRespVo WxCartAdd(@RequestBody WxCartAddBO wxCartAddBO){
//        Integer a =1;

//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        wxCartService.addCart(wxCartAddBO,activeUser.getId());
        Integer goodsCount = wxCartService.WxCartGoodsCount(activeUser.getId());
        return BaseRespVo.ok(goodsCount);
    }

    @RequestMapping("/delete")
    public BaseRespVo WxCartGoodsDelete(@RequestBody Map<String,Integer[]> map){
//        Integer a =1;
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        WxItemsBean<WxCart> wxCartBean = wxCartService.WxCartDelete(map,activeUser.getId());
        return BaseRespVo.ok(wxCartBean);
    }

    @RequestMapping("/checkout")
    public BaseRespVo WxCartCheckout(Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId){
//        Integer a =1;
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        CartCheckoutVO cartCheckoutVO = wxCartService.WxCartCheckout(cartId, addressId, couponId, grouponRulesId,activeUser.getId());
        return BaseRespVo.ok(cartCheckoutVO);
    }

    @RequestMapping("/fastadd")
    public BaseRespVo WxCartFastAdd(@RequestBody WxCartAddBO wxCartAddBO){
//        Integer a =1;
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User activeUser = (User) SecurityUtils.getSubject().getPrincipal();
        Integer cartId = wxCartService.WxCartFastAdd(wxCartAddBO,activeUser.getId());
        return BaseRespVo.ok(cartId);
    }
}